package propertymgmt.gui;

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JDesktopPane;

import propertymgmt.util.PropertiesManager;
import propertymgmt.util.WindowUtils;

/**
 * PropertyManager.java
 * 
 * Top-level GUI for application.
 *
 * @author Brian
 */
@SuppressWarnings("serial")
public class PropertyManager extends javax.swing.JFrame {
	
	private File currentDbFile = null;

    /** Creates new form PropertyManager */
    public PropertyManager() {
    	currentDbFile = new File("./database.odb");
    	PropertiesManager.getInstance().setCurrentDbFile(currentDbFile);
    	
        initComponents();
    }
    
    public JDesktopPane getTheDesktop(){
    	return desktopPane;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
    	
    	Dimension d = new Dimension(1200, 1000);
    	
    	setPreferredSize(d);
    	
        desktopPane = new javax.swing.JDesktopPane();
        menuBar = new javax.swing.JMenuBar();
        mnu_File = new javax.swing.JMenu();
        mnuItem_newProperty = new javax.swing.JMenuItem();
        mnuItem_newLeaseTerm = new javax.swing.JMenuItem();
        mnuItem_openProperty = new javax.swing.JMenuItem();
        mnuItem_newPayment = new javax.swing.JMenuItem();
        mnuItem_genReport = new javax.swing.JMenuItem();
        mnuItem_quit = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Property Manager");
        getContentPane().add(desktopPane, java.awt.BorderLayout.CENTER);

        mnu_File.setText("File");

        mnuItem_newProperty.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.ALT_MASK));
        mnuItem_newProperty.setText("Create New Property");
        mnuItem_newProperty.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuItem_newPropertyActionPerformed(evt);
            }
        });
        mnu_File.add(mnuItem_newProperty);

        mnuItem_newLeaseTerm.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.ALT_MASK));
        mnuItem_newLeaseTerm.setText("Create New Lease Term");
        mnuItem_newLeaseTerm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuItem_newLeaseTermActionPerformed(evt);
            }
        });
        mnu_File.add(mnuItem_newLeaseTerm);

        mnuItem_openProperty.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.ALT_MASK));
        mnuItem_openProperty.setText("Browse / Modify Properties");
        mnuItem_openProperty.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuItem_openPropertyActionPerformed(evt);
            }
        });
        mnu_File.add(mnuItem_openProperty);
        
        mnuItem_newPayment.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.ALT_MASK));
        mnuItem_newPayment.setText("Create New Payment");
        mnuItem_newPayment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuItem_newPaymentActionPerformed(evt);
            }
        });
        mnu_File.add(mnuItem_newPayment);
        
        mnuItem_genReport.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.ALT_MASK));
        mnuItem_genReport.setText("Generate Report");
        mnuItem_genReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuItem_genReportActionPerformed(evt);
            }
        });
        mnu_File.add(mnuItem_genReport);

        mnuItem_quit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.ALT_MASK));
        mnuItem_quit.setText("Quit");
        mnuItem_quit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuItem_quitActionPerformed(evt);
            }
        });
        mnu_File.add(mnuItem_quit);

        menuBar.add(mnu_File);

        setJMenuBar(menuBar);
        
        addWindowListener(new WindowAdapter() {
        	public void windowClosing(WindowEvent evt){
        		PropertiesManager.killInstance();
        	}
		});

        pack();
    }// </editor-fold>

    private void mnuItem_newPropertyActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        CreateNewProperty newProperty = new CreateNewProperty();
        newProperty.setVisible(true);
        desktopPane.add(newProperty);
        newProperty.moveToFront();
        WindowUtils.centerWithinParent(newProperty, desktopPane);
    }                                                   

    private void mnuItem_openPropertyActionPerformed(java.awt.event.ActionEvent evt) {                                                     
        BrowseModifyProperty openModifyProperty = new BrowseModifyProperty();
        openModifyProperty.setVisible(true);
        desktopPane.add(openModifyProperty);
        openModifyProperty.moveToFront();
        WindowUtils.centerWithinParent(openModifyProperty, desktopPane);
    }                                                    
    
    private void mnuItem_genReportActionPerformed(java.awt.event.ActionEvent evt) {
    	ReportGenFrame repGenFrame = new ReportGenFrame();
    	repGenFrame.setVisible(true);
    	desktopPane.add(repGenFrame);
    	repGenFrame.moveToFront();
    	WindowUtils.centerWithinParent(repGenFrame, desktopPane);
    }

    private void mnuItem_quitActionPerformed(java.awt.event.ActionEvent evt) {                                           
        dispose();
    }        
    
    private void mnuItem_newPaymentActionPerformed(java.awt.event.ActionEvent evt) {
    	CreateNewPayment newPayment = new CreateNewPayment();
    	newPayment.setVisible(true);
        desktopPane.add(newPayment);
        newPayment.moveToFront();
        WindowUtils.centerWithinParent(newPayment, desktopPane);
    }

    private void mnuItem_newLeaseTermActionPerformed(java.awt.event.ActionEvent evt) {
        CreateNewLeaseTerm newleaseTerm = new CreateNewLeaseTerm();
        newleaseTerm.setVisible(true);
        desktopPane.add(newleaseTerm);
        newleaseTerm.moveToFront();
        WindowUtils.centerWithinParent(newleaseTerm, desktopPane);
    }

    // Variables declaration - do not modify
    private javax.swing.JDesktopPane desktopPane;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem mnuItem_genReport;
    private javax.swing.JMenuItem mnuItem_newLeaseTerm;
    private javax.swing.JMenuItem mnuItem_newPayment;
    private javax.swing.JMenuItem mnuItem_newProperty;
    private javax.swing.JMenuItem mnuItem_openProperty;
    private javax.swing.JMenuItem mnuItem_quit;
    private javax.swing.JMenu mnu_File;
    // End of variables declaration
}